import os
import re
import shutil
import urllib2
import datetime

import source

class malwaredomains(source.Base):
  
  def __init__(self):
    self.retrieved = False
    self.url = 'http://mirror1.malwaredomains.com/files/domains.txt'
    self.dir = os.path.join('log', 'blacklists', str(datetime.date.today()))
    self.filename = os.path.join(self.dir, 'malwaredomains.com')
  
  @property 
  def name(self):
    return 'malwaredomains.com'
  
  def retrieve(self):
    if not os.path.exists(self.dir):
      os.makedirs(self.dir)

    req = urllib2.urlopen(self.url)
    with open(self.filename, 'wb') as f:
      shutil.copyfileobj(req, f)

    self.retrieved = True
    return  
    
  @property
  def domains(self):
    if not self.retrieved:
      self.retrieve()
    
    assert self.retrieved
    
    pattern = '(?:\d+\s+)?' + '(?P<domain>\S+)\s+' + '(?:(?P<type>\S+)\s+(?P<source>\S+)\s+)?' + '(?:\d+)+\s*'
    regex = re.compile(pattern)
    
    with open(self.filename) as f:
      for line in f:
        s = line.strip()
        if s == '' or s[0] == "#":
          continue
      
        match = regex.match(s)
        if match:
          d = match.groupdict()
          yield {"domain": d["domain"], "label": d["type"]}

  
    
